﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AgriculturalMachinery.Models
{
    public class AccountViewModel<T> : List<T>
    {
        #region 用户(账号)管理的分页代码

        public int AccountPageIndex { get; private set; }
        public int AccountPageSize { get; private set; }
        public int AccountTotalCount { get; private set; }
        public int AccountTotalPages { get; private set; }
        public IQueryable QueryClient { get; private set; }        
        public Clients client { get; private set; }
        public IQueryable QueryNotify { get; private set; }
        public bool AccountHasPreviousPage
        {
            get { return (AccountPageIndex > 0); }
        }
        public bool AccountHasNextPage
        {
            get { return (AccountPageIndex + 1 < AccountTotalPages);}
        }
        public AccountViewModel(Clients c)
        {
            client = c;
        }
        /// <summary>
        /// 创建可分页的Client对象
        /// </summary>
        /// <param name="AccountSource">传入Client对象以计算出Client在数据库中的总条目</param>
        /// <param name="AccountPageIndex">当前页索引</param>
        /// <param name="AccountPageSize">每一页大小</param>
        public AccountViewModel(IQueryable<T> AccountSource, int AccountPageIndex, int AccountPageSize)
        {
            NotifyRepository nr = new NotifyRepository();
            QueryNotify = nr.FindAllNotify();

            this.AccountPageIndex = AccountPageIndex;
            this.AccountPageSize = AccountPageSize;
            this.AccountTotalCount = AccountSource.Count();
            // 计算总页数 = 总条目数 / 每页大小大小
            // 如: 总条目数为16, 每页大小为7
            // 则总页数 = 16 / 7 = 2.28 , 取大于或等于该数字的十进制最小整数 3
            // 即总页数 = 3 (说明16条记录, 每页大小为7, 可分三页)
            this.AccountTotalPages = (int)Math.Ceiling(AccountTotalCount / (double)AccountPageSize);
            // Ceiling(Decimal) 返回大于或等于指定的十进制数的最小整数
            // Ceiling(Double) 返回大于或等于该指定双精度浮点数的最小整数。

            // 返回以分页的Client对象, skip方法跳过了 (当前页索引*每页大小) 条记录
            // take方法返回每页大小条记录, 实现了分页
            // 如: 当前页索引为0(即第一页), 每页大小为7,
            // skip方法跳过了0条记录 (页面索引为1时则跳过1*7条, 依此类推)
            // take方法返回7条
            QueryClient = AccountSource.Skip(AccountPageIndex * AccountPageSize).Take(AccountPageSize);

        }
        #endregion
    }
}
